package com.tl.boot.service.role.impl;

import com.tl.boot.common.enums.StatusEnum;
import com.tl.boot.common.utils.DateUtil;
import com.tl.boot.common.utils.LogUtil;
import com.tl.boot.entity.role.domain.RoleDetailInfo;
import com.tl.boot.entity.role.domain.RoleInfo;
import com.tl.boot.entity.role.domain.RoleResBindInfo;
import com.tl.boot.entity.role.dto.AddRoleInfoDTO;
import com.tl.boot.entity.role.dto.ListRoleInfoDTO;
import com.tl.boot.entity.role.dto.UpdateRoleInfoDTO;
import com.tl.boot.entity.role.dto.UpdateRoleResBindInfoDTO;
import com.tl.boot.entity.role.vo.ListRoleInfoVO;
import com.tl.boot.entity.role.vo.QueryRoleInfoVO;
import com.tl.boot.mapper.role.RoleMapper;
import com.tl.boot.service.role.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * com.tl.boot.service.role.impl -- RoleServiceImpl
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/18 18:49
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Long addRoleInfo(AddRoleInfoDTO dto) {
        RoleInfo roleInfo = new RoleInfo();
        BeanUtils.copyProperties(dto, roleInfo);
        roleMapper.addRoleInfo(roleInfo);
        return roleInfo.getRoleId();
    }

    @Override
    public List<ListRoleInfoVO> listRoleInfo(ListRoleInfoDTO dto) {
        RoleInfo roleInfo = new RoleInfo();
        BeanUtils.copyProperties(dto, roleInfo);
        return roleMapper.listRoleInfo(roleInfo);
    }

    @Override
    public void delRoleInfo(Long roleId) {
        roleMapper.delRoleInfo(roleId);
    }

    @Override
    public void updateRoleInfo(UpdateRoleInfoDTO dto) {
        RoleInfo roleInfo = new RoleInfo();
        BeanUtils.copyProperties(dto, roleInfo);
        roleMapper.updateRoleInfo(roleInfo);
    }

    @Override
    public QueryRoleInfoVO queryRoleInfo(Long roleId) {
        RoleDetailInfo roleDetailInfo = roleMapper.queryRoleInfo(roleId);
        if (roleDetailInfo != null) {
            QueryRoleInfoVO queryRoleInfoVO = new QueryRoleInfoVO();
            BeanUtils.copyProperties(roleDetailInfo, queryRoleInfoVO);
            queryRoleInfoVO.setRoleStatusEnum(StatusEnum.
                    getInstanceByCode(roleDetailInfo.getRoleStatus().intValue()));
            queryRoleInfoVO.setCreateTime(DateUtil.formatDate(roleDetailInfo.getCreateTime(),
                    DateUtil.FullFormatEnum.YMDHMS1));
            return queryRoleInfoVO;
        }
        return null;
    }

    @Override
    public void updateRoleResourceBindInfo(UpdateRoleResBindInfoDTO dto) {
        List<Long> addResIds = dto.getAddResIds();
        List<Long> delResIds = dto.getDelResIds();
        //下面的操作做异步操作
        handlerRoleResourceBindInfo(addResIds, delResIds, dto.getRoleId());
    }

    @Async
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    private void handlerRoleResourceBindInfo(List<Long> addResIds, List<Long> delResIds, Long roleId) {
        LogUtil.debug(String.format(
                "start async handler role_resource bind info...roleId>>>%s", roleId));
        if (addResIds != null && addResIds.size() > 0) {
            LogUtil.debug(String.format("addResIds>>>%s", addResIds.toString()));
            List<RoleResBindInfo> roleResBindInfos = addResIds.stream().map(
                    e -> new RoleResBindInfo(null, roleId, e, Short.valueOf("1"))).
                    collect(Collectors.toList());
            roleMapper.insertRoleResBindInfo(roleResBindInfos);
        }
        if (delResIds != null && delResIds.size() > 0) {
            LogUtil.debug(String.format("delResIds>>>%s", delResIds.toString()));
            roleMapper.delRoleResBindInfo(roleId, delResIds);
        }
    }
}
